package cn.com.scan.activity;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.text.method.NumberKeyListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import cn.com.scan.R;
import cn.com.scan.adapter.ExportItemAdapter;
import cn.com.scan.adapter.UsecheckAdapter;
import cn.com.scan.config.Config;
import cn.com.scan.entity.ApplicationItem;
import cn.com.scan.entity.ExportItem;
import cn.com.scan.entity.UseCheck;
import cn.com.scan.jsonparser.ParseJson;
import cn.com.scan.netTools.RequestWebServive;
import cn.com.scan.netTools.ResultCode;
import cn.com.scan.tools.DialogUtils;

/**
 * Created by My on 2016/1/2.
 */
public class UseCheckActivity extends BaseActivity {
    private String ReturnApplicationList = "SubmitUseInfo";//出库清单
    private int ReturnApplicationListTAG = 100;
    private ListView listView;
    private TextView user_name,keshi_name;
    private EditText input_num;
    private Button search_button;
    UsecheckAdapter usecheckAdapter;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            DialogUtils.closeProgressDialog();
            if (msg.what == ResultCode.SUCCESS) {
                handlerDisPose(msg);
            } else if (msg.what == ResultCode.IOFAILCODE) {
                Toast.makeText(UseCheckActivity.this, "服务器连接失败！", Toast.LENGTH_SHORT).show();
            } else if (msg.what == ResultCode.PARSERFAILCODE) {
                Toast.makeText(UseCheckActivity.this, "数据解析出错！", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(UseCheckActivity.this, "未知错误！", Toast.LENGTH_SHORT).show();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.use_check_activity);
        user_name = (TextView) findViewById(R.id.user_name);
        listView = (ListView) findViewById(R.id.list_view);
        input_num=(EditText)findViewById(R.id.input_num);
        search_button= (Button) findViewById(R.id.search_button);
        keshi_name = (TextView) findViewById(R.id.keshi_name);
        search_button.setOnClickListener(onclick);
        View headView = LayoutInflater.from(this).inflate(R.layout.user_pandian_item, null, false);
        headView.setBackgroundResource(R.color.blue);
        usecheckAdapter = new UsecheckAdapter(this);
        listView.addHeaderView(headView);
        user_name.setText(user.getNAME());
        keshi_name.setText(user.getDEPT_NAME());
//        HashMap<String, Object> map = new HashMap<String, Object>();
//        map.put("barcode", "YWT012345567890123456783");
//        map.put("user_name", user.getUSER_NAME());
//        map.put("dept_code", user.getDEPT_CODE());
//        requestNet(ReturnApplicationList, map, ReturnApplicationListTAG);
    }
    public View.OnClickListener  onclick=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("barcode", input_num.getText().toString());
            map.put("user_name", user.getUSER_NAME());
            map.put("dept_code", user.getDEPT_CODE());
            requestNet(ReturnApplicationList, map, ReturnApplicationListTAG);
        }
    };
    @Override
    protected void broadcastExit() {
        this.finish();
    }

    @Override
    protected void capturePic(String codeStr) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("barcode", codeStr);
            map.put("user_name",user.getUSER_NAME());
            map.put("dept_code",user.getDEPT_CODE());
            requestNet(ReturnApplicationList, map, ReturnApplicationListTAG);
    }

    public void requestNet(String method, HashMap<String, Object> map, int TAG) {
        DialogUtils.showProgressDialog(UseCheckActivity.this);
        RequestWebServive requestWebServive = new RequestWebServive(method, handler, map, TAG);
        requestWebServive.start();
    }

    private void handlerDisPose(Message msg) {
        String str = (String) msg.obj;
        if (!TextUtils.isEmpty(str)) {
            try {
                JSONObject jsonObject = new JSONObject(str);
                int code = jsonObject.getInt("CODE");
                if (code == 0 & !TextUtils.isEmpty(str)) {
                    String RESULT = jsonObject.getString("RESULT");
                    Config.LogI("RESULT-->"+RESULT);
                    if (msg.arg1 == ReturnApplicationListTAG) {
                        listView.setVisibility(View.VISIBLE);
                        listView.setAdapter(usecheckAdapter);
                        List<UseCheck> useChecks = ParseJson.parseUseCheck(RESULT);
                        usecheckAdapter.setList(useChecks);
                    }
                } else {
                    Toast.makeText(UseCheckActivity.this, jsonObject.getString("MSG"), Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}

